<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      class="search-form-new"
      :inline="true"
      v-show="showSearch"
      label-width="auto"
    >
      <el-form-item label="销售渠道" prop="saleChannel">
        <el-select
          v-model="queryParams.saleChannel"
          filterable
          placeholder="请选择销售渠道"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in dict.type.sale_user"
            :key="dict.label"
            :label="dict.label"
            :value="dict.label"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="供货渠道" prop="supplyChannel">
        <el-select
          v-model="queryParams.supplyChannel"
          filterable
          placeholder="请选择供货渠道"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in supplyArr"
            :key="dict.userId"
            :label="dict.nickName"
            :value="dict.nickName"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建人" prop="supplyId" class="mm-b-8">
        <el-select
          v-model="queryParams.createId"
          placeholder="请选择创建人"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in supplyList"
            :key="dict.user_id"
            :label="dict.user_name"
            :value="dict.user_id"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="账号" prop="account">
        <el-input
          v-model="queryParams.account"
          placeholder="请输入账号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品编号" prop="accountid">
        <el-input
          v-model="queryParams.accountid"
          placeholder="请输入账号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="游戏" prop="games">
        <el-select
          v-model="queryParams.games"
          placeholder="请选择游戏"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in gameList"
            :key="dict.id"
            :label="dict.name"
            :value="dict.name"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="销售是否支付" prop="ifPay">
        <el-select
          v-model="queryParams.ifPay"
          placeholder="请选择是否支付"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in dict.type.ifpay"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="供货是否支付" prop="supllypay">
        <el-select
          v-model="queryParams.supllypay"
          placeholder="请选择是否支付"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in dict.type.ifpay"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="supllypay">
        <el-select
          v-model="queryParams.oderstatus"
          placeholder="请选择状态"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in oderstatusList"
            :key="dict.id"
            :label="dict.name"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime" class="small">
        <el-date-picker
          v-model="saleTimeArr"
          size="small"
          format="yyyy-MM-dd"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
      <!-- <el-form-item>
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          v-hasPermi="['account:rec:edit']"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          >修改</el-button
        >
      </el-form-item>
      <el-form-item>
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['account:rec:remove']"
          >删除</el-button
        >
      </el-form-item>
      <el-form-item>
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['account:rec:export']"
          >导出</el-button
        >
      </el-form-item>
    </el-form>
    <!-- <div>
      <span>总金额：{{sumData.salePrice?sumData.salePrice.toFixed(2):'0.00' }}</span> <span class="shuxian">||</span>
      <span>真实销售总金额：{{ sumData.saleDisPrice?sumData.saleDisPrice.toFixed(2):'0.00' }}</span> <span class="shuxian">||</span>
      <span>供货总金额：{{ sumData.supplyPrice?sumData.supplyPrice.toFixed(2):'0.00' }}</span><span class="shuxian">||</span>
      <span>真实供货总金额：{{ sumData.supplydisPrice?sumData.supplydisPrice.toFixed(2):'0.00' }}</span><span class="shuxian">||</span>
      <span>利润总金额：{{ sumData.profitsSum?sumData.profitsSum.toFixed(2):'0.00' }}</span>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </div> -->

    <el-table
      v-loading="loading"
      :data="recList"
      @selection-change="handleSelectionChange"
      border
    >
      <el-table-column
        type="selection"
        width="55"
        align="center"
        fixed="left"
      />
      <el-table-column label="序号" align="center" prop="id" fixed="left" />
      <el-table-column
        label="商品编号"
        align="center"
        prop="accountid"
        fixed="left"
      />
      <el-table-column
        label="游戏类型"
        align="center"
        prop="games"
        fixed="left"
      />
      <el-table-column label="账号" align="center" prop="account" width="120" />
      <el-table-column
        label="销售渠道"
        align="center"
        prop="saleChannel"
        width="120"
      ></el-table-column>
      <el-table-column
        label="回收人"
        align="center"
        prop="recyclerName"
        width="120"
      ></el-table-column>
      <el-table-column label="原价" align="center" prop="oldPrice" />
      <el-table-column label="销售金额" align="center" prop="salePrice" />
      <el-table-column label="销售折扣" align="center" prop="saleDiscount" />
      <el-table-column label="接口折扣" align="center" prop="jkzk" />
      <el-table-column label="优惠金额" align="center" prop="discountAmount" />
      <el-table-column label="实收结算" align="center" prop="zdPrice">
        <!-- <template slot-scope="scope">
          <span>{{ scope.row.costAmount?Number(scope.row.costAmount)+Number(scope.row.profits):scope.row.profits}}</span>
        </template> -->
      </el-table-column>
      <el-table-column label="回收总金额" align="center" prop="costAmount" />
      <el-table-column label="利润" align="center" prop="profits" />

      <el-table-column label="状态" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.oderstatus == '1'">正常</span>
          <span v-if="scope.row.oderstatus == '2'" style="color: #ff6d6d"
            >销毁</span
          >
          <span v-if="scope.row.oderstatus == '3'" style="color: #ff6d6d"
            >处理中</span
          >
        </template>
      </el-table-column>
      <el-table-column
        label="换绑类型"
        width="150"
        align="center"
        prop="bindStatus"
      />
      <el-table-column label="备注" width="200" align="center" prop="remake" />
      <el-table-column
        label="回收时间"
        width="200"
        align="center"
        prop="recycleTime"
      />
      <el-table-column label="销售人" align="center" prop="saleUserName" />
      <el-table-column label="创建人" align="center" prop="createName" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="160"
      />

      <el-table-column
        label="换绑状态"
        align="center"
        prop="supllypay"
        fixed="right"
        width="100"
      >
        <template slot-scope="scope">
          <div
            v-if="scope.row.isBindStatus == 0"
            style="
              width: 62px;
              height: 26px;
              line-height: 26px;
              text-align: center;
              background-color: #e8f4ff;
              border-radius: 5px;
              margin: 0 auto;
              border: 1px solid #d1e9ff;
              color: #7cbfff;
              cursor: pointer;
            "
          >
            未换绑
          </div>
          <div
            v-else
            style="
              width: 62px;
              height: 26px;
              line-height: 26px;
              text-align: center;
              background-color: #fef0f0;
              border-radius: 5px;
              margin: 0 auto;
              border: 1px solid #f593a8;
              color: #f593a8;
              cursor: pointer;
            "
          >
            已换绑
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="供货是否支付"
        align="center"
        prop="supllypay"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.supllypay"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-value="1"
            inactive-value="2"
            @change="PayChange($event, scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column
        label="销售是否支付"
        align="center"
        prop="ifPay"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.ifPay"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-value="1"
            inactive-value="2"
            @change="PayChange($event, scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="115"
        fixed="right"
      >
        <template slot-scope="scope">
          <!-- <el-button size="mini" type="text" v-hasPermi="['account:rec:edit']" icon="el-icon-edit"
            @click="handleUpdate(scope.row)">修改</el-button> -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >

          <!-- v-hasPermi="['account:rec:remove']" -->
          <el-button
            v-if="scope.row.isBindStatus == 0"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="bindAccount(scope.row)"
            v-hasPermi="['ChangeBinding:rec:rdit']"
            >换绑</el-button
          >
          <el-button
            v-if="scope.row.isBindStatus == 1"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="bindAccountDetail(scope.row)"
            v-hasPermi="['ChangeBinding:rec:rdit']"
            >查看换绑详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改销售订单记录对话框 -->
    <!--

      <el-table-column label="商品编号" align="center" prop="accountid" fixed="left"/>
      <el-table-column label="游戏类型" align="center" prop="games" fixed="left"/>
      <el-table-column label="账号" align="center" prop="account" width="120"/>
      <el-table-column label="销售渠道" align="center" prop="saleChannel" width="120"></el-table-column>
      <el-table-column label="回收人" align="center" prop="recyclerName" width="120"></el-table-column>
      <el-table-column label="原价" align="center" prop="oldPrice" />
      <el-table-column label="销售金额" align="center" prop="salePrice" />
      <el-table-column label="销售折扣" align="center" prop="saleDiscount" />
      <el-table-column label="接口折扣" align="center" prop="jkzk" />
      <el-table-column label="优惠金额" align="center" prop="discountAmount" />
      <el-table-column label="实收结算" align="center" prop="zdPrice" >

      </el-table-column>
      <el-table-column label="成本金额" align="center" prop="costAmount" />
      <el-table-column label="利润" align="center" prop="profits" />
     -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="auto"
        :inline="true"
      >
        <el-form-item label="销售渠道" prop="saleChannel">
          <el-select
            v-model="form.saleChannel"
            placeholder="请选择销售渠道"
            style="width: 202px"
          >
            <el-option
              v-for="dict in dict.type.sale_user"
              :key="dict.label"
              :label="dict.label"
              :value="dict.label"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="供货渠道" prop="supplyChannel">
          <el-select
            v-model="form.supplyChannel"
            placeholder="请选择供货渠道"
            disabled
            style="width: 202px"
          >
            <el-option
              v-for="dict in supplyArr"
              :key="dict.userId"
              :label="dict.nickName"
              :value="dict.nickName"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="销售人" prop="saleUserName">
          <el-select
            v-model="form.saleUserId"
            placeholder="请选择销售人"
            style="width: 202px"
            filterable
          >
            <el-option
              v-for="(dict, index) in allUsers"
              :key="index"
              :label="dict.nickName"
              :value="dict.userId"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="商品编号" prop="salePrice">
          <el-input
            v-model="form.accountid"
            disabled
            placeholder="请输入商品编号"
          />
        </el-form-item>
        <el-form-item label="自编号" prop="sumNum">
          <el-input v-model="form.sumNum" disabled placeholder="请输入自编号" />
        </el-form-item>
        <el-form-item label="游戏类型" prop="games">
          <el-input v-model="form.games" disabled placeholder="游戏类型" />
        </el-form-item>
        <el-form-item label="账号" prop="account">
          <el-input v-model="form.account" disabled placeholder="请输入账号" />
        </el-form-item>
        <el-form-item label="回收总金额" prop="costAmount">
          <el-input v-model="form.costAmount" disabled placeholder="成本金额" />
        </el-form-item>
        <el-form-item label="原价" prop="oldPrice">
          <el-input v-model="form.oldPrice" disabled placeholder="原价" />
        </el-form-item>
        <el-form-item label="销售金额" prop="salePrice">
          <el-input v-model="form.salePrice" placeholder="销售金额" />
        </el-form-item>
        <el-form-item label="销售折扣" prop="saleDiscount">
          <el-input
            v-model="form.saleDiscount"
            disabled
            placeholder="销售折扣"
          />
        </el-form-item>
        <el-form-item label="接口折扣" prop="jkzk">
          <el-input v-model="form.jkzk" disabled placeholder="接口折扣" />
        </el-form-item>
        <el-form-item label="优惠金额" prop="discountAmount">
          <el-input v-model="form.discountAmount" placeholder="优惠金额" />
        </el-form-item>
        <el-form-item label="实收金额" prop="zdPrice">
          <el-input v-model="form.zdPrice" placeholder="实收金额" />
        </el-form-item>
        <el-form-item label="利润" prop="profits">
          <el-input v-model="form.profits" placeholder="利润" />
        </el-form-item>

        <el-form-item label="供货是否支付" prop="supllypay">
          <el-select
            v-model="form.supllypay"
            placeholder="请选择是否支付"
            disabled
            style="width: 202px"
          >
            <el-option
              v-for="dict in dict.type.ifpay"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="销售是否支付" prop="ifPay">
          <el-select
            v-model="form.ifPay"
            placeholder="请选择是否支付"
            disabled
            style="width: 202px"
          >
            <el-option
              v-for="dict in dict.type.ifpay"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="oderstatus">
          <el-select
            v-model="form.oderstatus"
            placeholder="请选择是否支付"
            style="width: 202px"
          >
            <el-option
              v-for="dict in oderstatusList"
              :key="dict.id"
              :label="dict.name"
              :value="dict.id"
            ></el-option>
          </el-select> </el-form-item
        ><br />
        <el-form-item label="备注" prop="remake">
          <el-input
            style="width: 600px"
            type="textarea"
            :rows="5"
            v-model="form.remake"
            placeholder="请输入备注"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 换绑 -->
    <el-dialog
      title="换绑"
      :visible.sync="dialogVisibleChangeBinding"
      width="50%"
      @close="onclose"
    >
      <el-form ref="form" :model="formData" label-width="100px">
        <el-form-item label="换绑类型">
          <el-select
            :disabled="Viewflag"
            v-model="formData.bindStatus"
            placeholder="请选择换绑类型"
          >
            <el-option
              v-for="(item, index) in buyerList"
              :key="index"
              :label="item.dictLabel"
              :value="item.dictLabel"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="换绑手机号">
          <el-input
            :disabled="Viewflag"
            style="width: 300px"
            v-model="formData.buyerPhone"
            placeholder="请输入买家换绑手机号"
          ></el-input>
        </el-form-item>
        <el-form-item label="换绑截图" prop>
          <div class="flex">
            <el-upload
              :disabled="Viewflag"
              :action="uploadImgUrl"
              :file-list="buyerImgList"
              :show-file-list="true"
              list-type="picture-card"
              multiple
              :on-success="handleAvatarSuccess"
              :on-remove="handleAvatarRemove"
              :on-preview="handlePictureCardPreview"
            >
              <i class="el-icon-plus"></i>
            </el-upload>
          </div>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="onclose">取 消</el-button>
        <el-button v-if="!Viewflag" type="primary" @click="bindStatusSure"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  listRec,
  getRec,
  createList,
  delRec,
  addRec,
  updateRec,
  exportRec,
} from "@/api/order/rec";
import moment from "moment";
import { getDicts } from "@/api/system/dict/data";
import { getSupplyList } from "@/api/info/info";
import { getUserProfile } from "@/api/system/user";
import gameListApi from "@/api/gameConfig/gameList";
import supplyConfigApi from "@/api/system/supplyConfig";

export default {
  name: "Rec",
  dicts: ["sale_user", "supply_user", "ifpay"],
  data() {
    return {
      gameList: [], //游戏列表
      supplyList: [],
      supplyArr: [],
      oderstatusList: [
        { name: "正常", id: "1" },
        { name: "销毁", id: "2" },
        { name: "处理中", id: "3" },
      ],
      sumData: {},
      supplyList: [],
      saleTimeArr: [],
      allUsers: [],
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 销售订单记录表格数据
      recList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        saleChannel: null,
        supplyChannel: null,
        saleDiscountPrice: null,
        saleDiscount: null,
        supplyDiscountPrice: null,
        supplyDiscount: null,
        salePrice: null,
        sumNum: null,
        profits: null,
        games: null,
        ifPay: null,
        isBindStatus: 0,
      },
      isAdmin: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      dialogVisibleChangeBinding: false,
      formData: {
        bindStatus: null,
        buyerPhone: null,
        buyerImg: null,
      },
      buyerImgList: [],
      buyerList: [],
      uploadImgUrl: "",
      Viewflag: false,
    };
  },
  created() {
    this.uploadImgUrl = process.env.VUE_APP_BASE_API + "/upload/image";
    this.getList();
    this.getSupplyList();
    this.getGameList();
    this.getAllUserList();
    this.getbuyerInfo();
  },
  methods: {
    // 图片预览
    handlePictureCardPreview(file) {
      this.$viewerApi({
        images: [file.url],
      });
    },
    handleAvatarRemove(e, file, fileList) {
      console.log("e,file", e, file);
      file.forEach((item) => {
        this.buyerImgList.push({ url: item.response.data.filePath });
      });
      // this.buyerImgList = file;
      this.formData.buyerImg = JSON.stringify(this.buyerImgList);
    },
    handleAvatarSuccess(e, file, fileList) {
      console.log("e,file", e, file, fileList);
      fileList.forEach((item) => {
        this.buyerImgList.push({ url: item.response.data.filePath });
      });
      // this.buyerImgList = fileList;
      // console.log("e,this.buyerImgList", this.buyerImgList);
      this.formData.buyerImg = JSON.stringify(this.buyerImgList);
    },

    bindAccount(item) {
      this.formData.id = item.id;
      this.dialogVisibleChangeBinding = true;
    },
    onclose() {
      this.Viewflag = false;
      this.formData = {
        bindStatus: null,
        buyerPhone: null,
        buyerImg: null,
      };
      this.dialogVisibleChangeBinding = false;
      this.buyerImgList = [];
    },
    async bindStatusSure() {
      const res = await supplyConfigApi.getbindiungInfo(this.formData);
      console.log(res);
      this.$message.success("操作成功!");
      this.getList();
      this.onclose();
    },
    async getbuyerInfo() {
      const res = await getDicts("bindOrderRec");
      this.buyerList = res.data;
      console.log("res", res);
    },
    bindAccountDetail(item) {
      console.log("item", item);
      this.Viewflag = true;
      this.dialogVisibleChangeBinding = true;
      this.formData.bindStatus = item.bindStatus;
      this.formData.buyerPhone = item.buyerPhone;
      this.buyerImgList = JSON.parse(item.buyerImg);
    },
    // 获取所有用户
    getAllUserList() {
      supplyConfigApi.getAllUserList().then((res) => {
        console.log("%c Line:104 🍯 所有用户res", "color:#93c0a4", res);
        this.allUsers = res.rows;
      });
    },
    // 游戏列表
    getGameList() {
      gameListApi.getGameList().then((res) => {
        this.gameList = res.rows;
      });
    },
    getSupplyList() {
      createList().then((res) => {
        if (res.code == 200) {
          this.supplyList = res.data;
        }
      });
      // 查询用户权限
      getUserProfile().then((response) => {
        if (response.roleGroup.indexOf("超级管理员") > -1) {
          console.log("超级管理员");
          this.isAdmin = true;
        } else {
          console.log("非超级管理员");
          this.isAdmin = false;
        }
      });

      getSupplyList("supplyCustomer").then((res) => {
        if (res.code == 200) {
          console.log(res);
          this.supplyArr = res.data;
        }
      });
    },
    inputSalePrice() {
      //获取销售折扣
      let saleDiscount = 0;
      let supplyDiscount = 0;

      let sale_user = [];
      getDicts("sale_user").then((res) => {
        if (res.code == 200) {
          sale_user = res.data;
          console.log(sale_user);
          for (var i = 0; i < sale_user.length; i++) {
            console.log(this.form.saleChannel);
            console.log(sale_user[i].dictLabel);
            if (this.form.saleChannel == sale_user[i].dictLabel) {
              saleDiscount = sale_user[i].dictValue;
              break;
            }
          }
          console.log(saleDiscount);

          getDicts("supply_user").then((res) => {
            if (res.code == 200) {
              supply_user = res.data;
              console.log(supply_user);
              for (var i = 0; i < supply_user.length; i++) {
                if (this.form.supplyChannel == supply_user[i].dictLabel) {
                  supplyDiscount = supply_user[i].dictValue;
                  break;
                }
              }
              console.log(supplyDiscount);

              //销售折扣
              this.form.saleDiscount = saleDiscount;
              //供货折扣
              this.form.supplyDiscount = supplyDiscount;

              //销售折扣后金额
              let saleDiscountPrice = (saleDiscount / 10) * this.form.salePrice;
              console.log(saleDiscountPrice);

              let supplyDiscountPrice =
                (supplyDiscount / 10) * this.form.salePrice;

              this.form.supplyDiscountPrice = supplyDiscountPrice;

              this.form.saleDiscountPrice = saleDiscountPrice;

              let profits = saleDiscountPrice - supplyDiscountPrice;

              //计算利润
              this.form.profits = profits.toFixed(2);
            }
          });
        }
      });

      //获取供货渠道折扣
      let supply_user = this.getDictData("supply_user");
      for (var i = 0; i < supply_user; i++) {
        if (this.form.supplyChannel == supply_user[i].dictLabel) {
          supplyDiscount = supply_user[i].dictValue;
          break;
        }
      }
    },
    getDictData(type) {
      let data = [];
      getDicts(type).then((res) => {
        if (res.code == 200) {
          data = res.data;
        }
      });
      return data;
    },
    /** 查询销售订单记录列表 */
    getList() {
      if (this.saleTimeArr.length > 1) {
        var startDate = moment(this.saleTimeArr[0]).format("YYYY-MM-DD");
        this.queryParams.saleTimeStart = startDate + " 00:00:00";
        var endDate = moment(this.saleTimeArr[1]).format("YYYY-MM-DD");
        this.queryParams.saleTimeEnd = endDate + " 23:59:59";
      }
      this.loading = true;
      listRec(this.queryParams).then((response) => {
        this.loading = false;
        console.log(response);
        if (response.params) {
          this.sumData = response.params;
        }
        this.recList = response.rows;
        this.total = response.total;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        createId: null,
        updateId: null,
        createTime: null,
        updateTime: null,
        saleChannel: null,
        supplyChannel: null,
        saleDiscountPrice: null,
        saleDiscount: null,
        supplyDiscountPrice: null,
        supplyDiscount: null,
        salePrice: null,
        sumNum: null,
        profits: null,
        games: "0",
        ifPay: null,
        isBindStatus: 0,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加销售订单记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getRec(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改销售订单记录";
      });
    },
    // 销售支付状态改变
    PayChange(e, row) {
      updateRec(row).then((response) => {
        this.$modal.msgSuccess("修改成功");
        this.getList();
      });
    },
    /** 提交按钮 */
    submitForm() {
      // this.form.saleUserName =  this.allUsers.filter(user => user.createId = this.form.saleUserId).nickName

      console.log(
        this.allUsers.filter((user) => user.createId == this.form.saleUserId)
      );

      console.log(this.form.saleUserId);

      for (var i = 0; i < this.allUsers.length; i++) {
        var user = this.allUsers[i];
        // console.log(user)
        if (this.form.saleUserId == user.userId) {
          this.form.saleUserName = user.nickName;
          console.log(user.nickName);
        }
      }

      console.log(this.form.saleUserName);

      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateRec(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRec(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除销售订单记录编号为"' + ids + '"的数据项？')
        .then(function () {
          return delRec(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal
        .confirm("是否确认导出所有销售订单记录数据项？")
        .then(() => {
          this.exportLoading = true;
          return exportRec(queryParams);
        })
        .then((response) => {
          this.$download.name(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
  },
};
</script>
<style scoped lang="scss">
.shuxian {
  padding: 0 8px;
}
</style>
